from JDjango.dialogs.MessageDialog.RichMessageDialog import RichMsgDialog
from .common import *

class ViewGeneratorPage1(wx.adv.WizardPage):
    '''个性化创建视图【第一页】'''
    def __init__(self, parent, title='第一步：选择视图类型和生成方式', out_data = None):
        WizardPageSimple.__init__(self, parent)
        self.next = self.prev = None
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(self.sizer)
        if out_data is not None:
            self.out_data = out_data
            if VIEWS_WIZARD_CONTEXT not in self.out_data:
                self.out_data[VIEWS_WIZARD_CONTEXT] = {}
            self.context = self.out_data[VIEWS_WIZARD_CONTEXT]
            self.context['1'] = {} # 表示第一页
            self.context = self.context['1']
        else:
            raise Exception('没有上下文，无法开启向导页面')

        self._init_UI(title)
        self._init_listener()

    def _init_UI(self, title):
        '''初始化界面布局'''
        '''
            设置当前页标题
        '''
        title = wx.StaticText(self, -1, title)
        title.SetFont(wx.Font(16, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
        self.sizer.Add(title, 0, wx.ALIGN_LEFT|wx.ALL, 3)
        self.sizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND|wx.ALL, 5) # 水平分割线

        '''
            所有的其他控件全部放在滚动面板中
        '''
        self.scollPanel = scrolledpanel.ScrolledPanel(self, -1, size=SIZE) # 定义滚动面板
        self.scollPanel.SetupScrolling() # 开启滚动
        self.scollPanelSizer = wx.BoxSizer(wx.VERTICAL)
        self.scollPanel.SetSizer(self.scollPanelSizer)
        self.sizer.Add(self.scollPanel, 1, wx.EXPAND | wx.ALL, 2) # 加入主容器

        self._init_scollpanel()

        self.tipInput = wx.TextCtrl(self, -1, value='', style=wx.TE_MULTILINE|wx.TE_READONLY, size=(-1, 48))
        self.sizer.Add(self.tipInput, 0, wx.EXPAND | wx.ALL, 0)

    def _init_scollpanel(self):
        '''初始化滚动窗口'''

        self.choiceViewType = wx.Choice(self.scollPanel, -1, choices=['', *view_type_data.get_all_views_type_name()])
        self.inputViewName = wx.TextCtrl(self.scollPanel, -1, value='')
        self.inputViewAliasName = wx.TextCtrl(self.scollPanel, -1, value='')
        
        self.radioBoxViewType = wx.RadioBox(self.scollPanel, -1, '视图类型', choices=['函数视图', '类视图', '未知',])
        self.radioBoxGenWay = wx.RadioBox(self.scollPanel, -1, '选择生成方式', choices=['模板生成', '结构生成', 'API生成', '其它', ])

        '''
            选择创建视图类型（网格布局）
        '''
        self.flexGridSizer = wx.FlexGridSizer(cols=3, hgap=5, vgap=5)
        self.scollPanelSizer.Add(self.flexGridSizer, 1, wx.EXPAND | wx.ALL, 5)
        self.flexGridSizer.AddGrowableCol(1)

        self.flexGridSizer.Add(wx.StaticText(self.scollPanel, -1, label='视图类型'), 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        self.flexGridSizer.Add(self.choiceViewType, 0, wx.EXPAND)
        label = wx.StaticText(self.scollPanel, -1, label='必选')
        label.SetForegroundColour(wx.RED)
        self.flexGridSizer.Add(label, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL)

        self.flexGridSizer.Add(wx.StaticText(self.scollPanel, -1, label='视图名称'), 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        self.flexGridSizer.Add(self.inputViewName, 0, wx.EXPAND)
        label = wx.StaticText(self.scollPanel, -1, label='必填')
        label.SetForegroundColour(wx.RED)
        self.flexGridSizer.Add(label, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL)

        self.flexGridSizer.Add(wx.StaticText(self.scollPanel, -1, label='路由名称'), 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        self.flexGridSizer.Add(self.inputViewAliasName, 0, wx.EXPAND)
        self.flexGridSizer.Add(wx.StaticText(self.scollPanel, -1, label='默认取视图名称小写'), 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL)

        self.flexGridSizer.Add((5,5))
        self.flexGridSizer.Add(self.radioBoxViewType, 0, wx.EXPAND)
        self.flexGridSizer.Add((5,5))

        self.flexGridSizer.Add((5,5))
        self.flexGridSizer.Add(self.radioBoxGenWay, 0, wx.EXPAND)
        self.flexGridSizer.Add((5,5))

    def _init_listener(self):
        """注册监听"""
        self.Bind(wx.EVT_CHOICE, self.onChoiceViewType, self.choiceViewType)
        self.Bind(wx.EVT_TEXT, self.onInputViewName, self.inputViewName)
        self.Bind(wx.adv.EVT_WIZARD_PAGE_CHANGING, self.onWizPageChanging)
        self.Bind(wx.adv.EVT_WIZARD_CANCEL, self.onWizCancel)

    def onWizCancel(self, evt):
        """界面取消时触发"""
        if not RichMsgDialog.showYesNoMsgDialog(self, '【取消】/【关闭】将导致已填的数据全部被丢弃！', "警告"):
            evt.Veto()

    def onInputViewName(self, evt):
        """视图名称录入实时监听"""
        self.inputViewAliasName.SetValue(evt.GetString().lower())

    def onChoiceViewType(self, evt):
        """响应类型选择事件"""
        view_type_name = evt.GetString().strip()
        if view_type_name:
            ### 底部提示信息框更新
            temp = view_type_data.get_listdata_by_view_type_name(view_type_name, 'tips')
            self.tipInput.SetValue('\n'.join(temp))

            ## 视图类型更新
            t_type = view_type_data.get_str_by_view_type_name(view_type_name, 'type')
            if t_type not in ('def','class',):
                self.radioBoxViewType.SetSelection(2)
            else:
                if "def" == t_type:
                    self.radioBoxViewType.SetSelection(0)
                else:
                    self.radioBoxViewType.SetSelection(1)
        else:
            self.tipInput.SetValue("")

    def onWizPageChanging(self, evt):
        """向导页切换时触发"""
        '''
            保存当前页面的上下文数据
        '''
        ### 视图类型名称【保存】
        if wx.NOT_FOUND != self.choiceViewType.GetSelection():
            self.context['view_type_name'] = self.choiceViewType.GetString(self.choiceViewType.GetSelection()).strip()
        else: self.context['view_type_name'] = ''

        ### 生成方式【保存】
        self.context['gen_way'] = self.radioBoxGenWay.GetString(self.radioBoxGenWay.GetSelection())
        self.context['view_type'] = self.radioBoxViewType.GetSelection()

        ### 视图名称、路由名称【保存】
        self.context['view_name'] = self.inputViewName.GetValue().strip()
        self.context['url_name'] = self.inputViewAliasName.GetValue().strip()

        '''
            校验
        '''
        if not self.context['view_type_name']:
            evt.Veto()
            RichMsgDialog.showOkMsgDialog(self, '试图类型必选', '错误')
            return
        if not self.context['view_name']:
            evt.Veto()
            RichMsgDialog.showOkMsgDialog(self, '视图名称必填', '错误')
            return

    def SetNext(self, next):
        '''设置后一页'''
        self.next = next

    def SetPrev(self, prev):
        '''设置前一页'''
        self.prev = prev

    def GetNext(self):
        '''获取下一页/下一指定页'''
        return self.next

    def GetPrev(self):
        '''获取上一页/上一指定页'''
        return self.prev
